<template>
  <div class="flexContent">
    <div class="searchBox">
      <!-- 拣货单记录 -->
      <el-form ref="form" :model="form" label-position="left" label-width="auto" :inline="false">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="拣货单号" prop="chooseNumber">
                  <el-input v-clearEmoji v-model.trim="form.chooseNumber" placeholder="请输入拣货单号" style="width: 95%;"
                    clearable @keyup.enter.native="search" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="门店名称" prop="storeId">
                  <el-select v-model="form.storeId" placeholder="请选择门店" clearable filterable popper-class="eloption"
                    style="width: 95%;" @click.native="getShopList">
                    <!-- <el-option v-for="item in aisleOptions" :key="item.id" :value="item.id" :label="item.name" /> -->
                    <el-option v-for="item in shopOptions" :key="item.id" :value="item.id" :label="item.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="要货单号" prop="requireNumber">
                  <el-input v-clearEmoji v-model.trim="form.requireNumber" placeholder="请输入要货单号" style="width: 95%;"
                    clearable @keyup.enter.native="search" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="通道组名称" prop="groupName">
                  <el-input v-clearEmoji v-model.trim="form.groupName" placeholder="请输入通道组名称" style="width: 95%;"
                    clearable @keyup.enter.native="search" />
                </el-form-item>
              </el-col>
             
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="仓库名称" prop="warehouseId">
                  <el-select v-model="form.warehouseId" placeholder="请选择仓库" filterable clearable popper-class="eloption"
                    style="width: 95%;" @click.native="reqWarehouseList">
                    <el-option v-for="item in warehouseList" :key="item.id" :value="item.id" :label="item.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="打印时间" prop="printTime">
                  <el-date-picker v-model="form.printTime" style="width: 95%;" type="daterange" unlink-panels
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd"
                    clearable />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="search" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetForm('form')" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" align="center" style="width: 100%"
          height="100%" size="medium">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="warehouseName" title="仓库名称"
            :min-width="elTableAdaptationWidth('warehouseName', tableData)" />
          <vxe-column field="chooseNumber" title="拣货单号" :min-width="elTableAdaptationWidth('chooseNumber', tableData)" />
          <vxe-column field="requireNumber" title="要货单号"
            :min-width="elTableAdaptationWidth('requireNumber', tableData)" />
          <vxe-column field="groupName" title="通道组名称" min-width="200" />
          <vxe-column field="storeName" title="门店名称" :min-width="elTableAdaptationWidth('storeName', tableData)" />
          <vxe-column field="printTime" title="打印时间" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.printTime | parseTime }}</span>
            </template>
          </vxe-column>
          <vxe-column title="操作" fixed="right" width="150"
            v-if="$showFn(['packingorderrecord_detail', 'packingorderrecord_print'])">

            <template slot-scope="{row}">
              <el-button type="text" @click="repairPrint(row)" v-has="'packingorderrecord_print'">补打</el-button>
              <el-button type="text" @click="toShowDetail(row)" v-has="'packingorderrecord_detail'">详情</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination background :current-page.sync="page.pageNum" :page-sizes="$pageSizes"
          :page-size.sync="page.pageSize" layout="total,->, sizes, prev, pager, next, jumper" :total="page.total"
          @size-change="changePageSize" @current-change="getOrderRecordList" />
      </div>
    </div>
  </div>
</template>

<script>
import { getShopEasy } from '@/api/shop'
import { reqWarehouseList } from '@/api/warehouse'
import { reqOrderRecordList, reqOrderRecordById } from '@/api/packingOrderRecord'
import { getTableStr, print, checkPrinter } from '@/utils/printLODOP'
export default {
  name: 'PackingOrderRecord',
  data() {
    return {
      form: {
        chooseNumber: '', // 采购订单编号
        requireNumber: '', // 要货订单编号
        printTime: [], // 打印时间
        storeId: '', // 门店ID
        groupName: '', // 通道组名称
        warehouseId: '' // 仓库Id
      },
      isSearch: false,
      shopOptions: [],
      warehouseList: [],
      tableData: [],
      page: {
        pageSize: 20,
        pageNum: 1,
        total: 0
      },

      printObj: {},
    }
  },
  computed: {
    openTime() {
      if (this.form.printTime == null) {
        return "";
      }
      if (this.form.printTime.length > 0) {
        return this.form.printTime[0]
      } else {
        return ''
      }
    },
    endTime() {
      if (this.form.printTime == null) {
        return '';
      }
      if (this.form.printTime.length > 0) {
        return this.form.printTime[1]
      } else {
        return ''
      }
    }

  },
  created() {
    // this.getShopList();
    // this.reqWarehouseList();
    this.getOrderRecordList();
  },
  methods: {
    async reqWarehouseList() {
      const res = await reqWarehouseList()
      if (res.code === 200) {
        this.warehouseList = res.data
        console.log(this.warehouseList, 'Ware')
      }
    },
    async getShopList() {
      const res = await getShopEasy()
      if (res.code === 200) {
        this.shopOptions = res.data.store
      }
    },
    async getOrderRecordList() {
      const { pageNum, pageSize } = this.page
      const { endTime, openTime } = this
      let { chooseNumber, requireNumber, storeId, groupName, warehouseId } = this.form
      if (!this.isSearch) {
        chooseNumber = undefined
        requireNumber = undefined
        storeId = undefined
        groupName = undefined
        warehouseId = undefined
      }
      const reqObj = { pageNum, pageSize, chooseNumber, warehouseId, requireNumber, storeId, groupName, endTime, openTime }
      const res = await reqOrderRecordList(reqObj)
      if (res.code === 200) {
        console.log(res.data)
        this.tableData = res.data.list
        this.page.total = res.data.total
        this.page.pageSize = res.data.pageSize
        this.page.pageNum = res.data.pageNum
        const $table = this.$refs.xTable1
        $table.loadData(this.tableData);
      }
    },
    changePageSize() {
      this.page.pageNum = 1
      this.getOrderRecordList()
    },
    search() {
      this.page.pageNum = 1
      this.isSearch = true
      this.getOrderRecordList()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.page.pageNum = 1
      this.page.pageSize = 20
      this.isSearch = false
      this.getOrderRecordList()
    },
    repairPrint(row) {
      this.$confirm(`即将补打拣货单，是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        if (!checkPrinter())
          return;
        let res = await reqOrderRecordById({ id: row.id })
        if (res.code === 200) {
          this.printObj = res.data;
          print(this.printObj);
          this.$message({ type: 'success', message: '正在打印拣货单' })
        } else {
          // this.$message.error(res.message);
        }
      })
    },
    async toShowDetail(row) {
      let res = await reqOrderRecordById({ id: row.id })
      if (res.code === 200) {
        this.printObj = res.data;
      } else {
        // this.$message.error(res.message);
      }
      let str = `<div style="display:flex;justify-content: space-between;padding:0 10px">
          <h3>区域:${this.printObj.regionName}</h3>
          <h3>品规数:${this.printObj.commodityNum}</h3>
          <h3>第1页 共1页</h3>
        </div><div style="display:flex;justify-content: space-between;padding:0 10px">
          <h3>单号:${this.printObj.chooseNumber}</h3>
          <h3>通道组:${this.printObj.groupName}</h3>
        </div>`;
      this.$alert(str + `<div style="overflow-x:auto;max-height:400px;display:flex;justify-content: center;">${getTableStr(this.printObj)}</div>`, '拣货单详情', {
        dangerouslyUseHTMLString: true,
        customClass: 'alert-class',
        confirmButtonText: '关闭'
      });
    }
  }
}
</script>

<style>
.alert-class {
  width: 800px;
}
</style>
